package net.zhengxinyun.unifyApp.ctrl;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.unifyApp.Util.DateUtils;
import net.zhengxinyun.unifyApp.core.Result;
import net.zhengxinyun.unifyApp.core.ResultUtils;
import net.zhengxinyun.unifyApp.entity.message.DailyOperation;
import net.zhengxinyun.unifyApp.entity.message.OperaInfo;
import net.zhengxinyun.unifyApp.filter.message.DailyOperationFilter;
import net.zhengxinyun.unifyApp.filter.message.OperaInfoFilter;
import net.zhengxinyun.unifyApp.service.message.DailyOperationService;
import net.zhengxinyun.unifyApp.service.message.OperaInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/app")
public class MessageCtrl {

    @Autowired
    private OperaInfoService opService;
    @Autowired
    private DailyOperationService dailyOperationService;

    @ResponseBody
    @PostMapping("/message/operaInfo")
    public Result<HashMap<String, Object>> getOpreatInfo(){

        List<OperaInfo> operaInfos = opService.getOperaInfo(new OperaInfoFilter());

        HashMap<String,Object> data = new HashMap<>();
        for (OperaInfo item : operaInfos) {
            String replace = item.getUpdInfo().replace(";", "\n");
            item.setUpdInfo(replace);

        }
        data.put("data",operaInfos);

        return ResultUtils.getSuccessResults(data);

    }


    @GetMapping("/message/save")
    public Result<HashMap<String, Object>> saveOpreatInfo(){

        opService.saveOperaInfo();

        HashMap<String,Object> data = new HashMap<>();
        data.put("data",null);

        return ResultUtils.getSuccessResults(data);

    }

    /**
     * 查询每日成效
     * @param
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @GetMapping("/dailyOperation/queryAll")
    public Result<HashMap<String, Object>> queryAll(){
        List<DailyOperation> list = dailyOperationService.getDailyOperations();
        HashMap<String,Object> data = new HashMap<>();
        data.put("data",list);
        return ResultUtils.getSuccessResults(data);
    }

    /**
     * 新增每日成效
     * @param filter
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/dailyOperation/saveOne")
    public Result<HashMap<String, Object>> saveDailyOperation(@RequestBody DailyOperation filter){
        String dataTime = filter.getDataTime();
        DailyOperation dailyOperation = dailyOperationService.getDailyOperationByDataTime(dataTime);
        if(ObjectUtils.isBlank(dailyOperation)){
            dailyOperationService.saveDailyOperation(filter);
        }else{
            filter.setId(dailyOperation.getId());
            dailyOperationService.updateDailyOperation(filter);
        }

        HashMap<String,Object> data = new HashMap<>();
        data.put("data",null);
        return ResultUtils.getSuccessResults(data);
    }

    /**
     * 新增每日成效
     * @param filter
     * @return
     */
    @CrossOrigin
    @ResponseBody
    @PostMapping("/dailyOperation/getByDataTime")
    public Result<HashMap<String, Object>> getByDataTime(@RequestBody DailyOperationFilter filter){
        String dataTime = filter.getDataTime();
        DailyOperation dailyOperation = dailyOperationService.getDailyOperationByDataTime(dataTime);
        HashMap<String,Object> data = new HashMap<>();
        data.put("data",dailyOperation);
        return ResultUtils.getSuccessResults(data);
    }
}
